package com.atguigu.yygh.order.controller;


import com.atguigu.yygh.enums.OrderStatusEnum;
import com.atguigu.yygh.model.order.OrderInfo;
import com.atguigu.yygh.order.service.OrderInfoService;
import com.atguigu.yygh.result.R;
import com.atguigu.yygh.utils.JwtHelper;
import com.atguigu.yygh.vo.order.OrderCountQueryVo;
import com.atguigu.yygh.vo.order.OrderQueryVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-04-27
 */
@RestController
@RequestMapping("/api/orderInfo")
public class OrderInfoController {

    @Autowired
    private OrderInfoService orderInfoService;



    @GetMapping("/status/list")
    public R getPage(){
        List<Map<String, Object>> statusList = OrderStatusEnum.getStatusList();
        return R.ok().data("list",statusList);
    }


    @GetMapping("/{pageNum}/{limit}")
    public R getPage(@PathVariable Integer pageNum, @PathVariable Integer limit, OrderQueryVo orderQueryVo, @RequestHeader String token){
        Long userId = JwtHelper.getUserId(token);
        orderQueryVo.setUserId(userId);
        Page<OrderInfo> orderInfoPage= orderInfoService.getOrderPage(pageNum,limit,orderQueryVo);
        return R.ok().data("pageModel",orderInfoPage);
    }

    @PostMapping("/save/{scheduleId}/{patientId}")
    public R save(@PathVariable String scheduleId,
                  @PathVariable Long patientId){
        Long orderId= orderInfoService.saveOrderInfo(scheduleId,patientId);
       return R.ok().data("orderId",orderId);
    }

    //detail
    @GetMapping("/detail/{orderId}")
    public R detail(@PathVariable Long orderId,@RequestHeader String token){
        Long userId = JwtHelper.getUserId(token);
        OrderInfo orderInfo=orderInfoService.detail(userId,orderId);
        return R.ok().data("orderInfo",orderInfo);
    }



   @PutMapping("/cancel/{orderId}")
   public R cancelOrder(@PathVariable Long orderId){
     orderInfoService.cancelOrder(orderId);
     return R.ok();
   }



   @PostMapping("/statistics")
   public Map<String,Object> getStatistics(@RequestBody  OrderCountQueryVo orderCountQueryVo){
       return orderInfoService.getStatistics(orderCountQueryVo);
   }

}

